﻿using Common;
using DadayunApi.BLL;
using DadayunApi.Model;
using KhtApi.BLL;
using KhtApi.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KhtMaintain
{
    public partial class MainForm : Form
    {
        
        string settingFile = "settings.ini";
        MyConfiguration cfg = new MyConfiguration(true);
        AppAuthBase authBase = null;
        string baseUri = "";

       
        public MainForm()
        {
            InitializeComponent();
        }

        private void mnuSetSystem_Click(object sender, EventArgs e)
        {
           
            FrmSettings frm = new FrmSettings();
            if(frm.ShowDialog()==DialogResult.OK)
            {
                mnuFunction.Enabled = RefreshLoginInfo();
            }
        }

        private bool RefreshLoginInfo()
        {
            if (File.Exists(settingFile))
            {
                cfg.Load(settingFile);
                authBase = new AppAuthBase
                {
                    AppKey = cfg.ReadString(SettingItems.ddyAppKey),
                    AppSecret = cfg.ReadString(SettingItems.ddySecret),
                    Password = cfg.ReadString(SettingItems.ddyPasswd),
                    PublicKey = cfg.ReadString(SettingItems.ddyPubkey),
                    TenantName = cfg.ReadString(SettingItems.ddyTenantName),
                    Username = cfg.ReadString(SettingItems.ddyUsername)
                };
                baseUri = cfg.ReadString(SettingItems.ddyBaseUri);
               
                if (string.IsNullOrEmpty(baseUri) || !authBase.IsUsefull())
                {                   

                    MessageBox.Show("服务器配置或用户配置有缺失，请设置再试", "严重错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
                else
                {
                    try
                    {
                        UserInfoBLL bll = new UserInfoBLL(authBase, baseUri);
                        var login = bll.GetLoginInfo();
                        if(login!=null)
                        {
                            staLogin.Text = $"{login.PositionInfo.PositionName} {login.User.Name} 已登录";
                            return true;
                        }
                        else
                        {
                            //string msg = "无法获取用户信息，返回信息为：\r\n" + bll.ResponseJson
                            //                    + "\r\n请修改系统设置，并重启动程序试试";
                            //MessageBox.Show(msg, "致命错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {

                        string msg = "无法获取用户信息，返回信息为：\r\n" + ex.Message
                     + "\r\n请修改系统设置，并重启动程序试试";
                        MessageBox.Show(msg, "致命错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return false;
                    }
                   
                }
            }
            else
            {                
                MessageBox.Show("找不到配置文件！请设置再试", "严重错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
        private void MainForm_Load(object sender, EventArgs e)
        {

            if(RefreshLoginInfo())
            {
                mnuFunction.Enabled = true;
                MnuPMC_Click(sender, e);
            }
            else
            {
                mnuFunction.Enabled = false;
            }
          
            
        }
        public void SetStaInfo(string info)
        {
            staInfo.Text = info;
        }
        public void SetTitle(string text)
        {
            Text = $"KHT-Dadayun系统工具：{text}";
        }
        FrmBomFixProductInfo frmBomFixProduct = null;
        private void mnuUpdateBomPrdInfo_Click(object sender, EventArgs e)
        {
          
            if (frmBomFixProduct == null)
            {
                frmBomFixProduct = new FrmBomFixProductInfo(authBase, baseUri)
                {
                    TopLevel = false,
                    FormBorderStyle = FormBorderStyle.None,
                    Dock = DockStyle.Fill,
                    Tag = this//把主控界面传给调用页面，方便访问主控界面的资源
                };
                panel1.Controls.Add(frmBomFixProduct);
            }
            frmBomFixProduct.Show();
        }

        FrmBomFixMaterial frmBomFixMaterial = null;
        private void mnuUpdateBomMaterial_Click(object sender, EventArgs e)
        {
           
            if (frmBomFixMaterial == null)
            {
                frmBomFixMaterial = new FrmBomFixMaterial(authBase, baseUri)
                {
                    TopLevel = false,
                    FormBorderStyle = FormBorderStyle.None,
                     Dock = DockStyle.Fill,
                    Tag = this//把主控界面传给调用页面，方便访问主控界面的资源
                };
                panel1.Controls.Add(frmBomFixMaterial);
            }
            frmBomFixMaterial.Show();
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            //测试提交，看能成功更新不,成功啊，提交到哪去了？
            //为什么在IDE中不能提交？
            //克隆提交
            Close();
        }
        FrmPMC frmPMC = null;
        private void MnuPMC_Click(object sender, EventArgs e)
        {
            if (frmPMC == null)
            {
                frmPMC = new FrmPMC(authBase, baseUri)
                {
                    TopLevel = false,
                    FormBorderStyle = FormBorderStyle.None,
                    Dock = DockStyle.Fill,
                    Tag=this//把主控界面传给调用页面，方便访问主控界面的资源
                };
                panel1.Controls.Add(frmPMC);
            }
            frmPMC.Show();
        }
       
    }
}
